/*
    This file is part of jTopaz.
    http://gcl.cis.udel.edu/projects/jtopaz
    Copyright (C) 2009 University of Delaware

    jTopaz is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    jTopaz is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/

package jTopaz;

import org.ietf.jgss.GSSCredential;
import org.globus.myproxy.MyProxy;

/**
Handles logging onto a myproxy server to obtain a proxy credential.
*/
public class MyProxyThread extends Thread{
	
	private String host;
	private String username;
	private String password;
	private GSSCredential cred;
	private String status;
	private boolean error=false;
	
	/**
	@host the name of the myproxy host.
	@username the username.
	@password the account password
	*/
	public MyProxyThread(String host, String  username, String password) {
		
		this.host = host;
		this.username = username;
		this.password = password;
	}
	
	/**
	Attempts to log onto the myproxy server, and then get the credential
	*/
	public void run() {
	
		try {
			System.out.println("trying to make my proxy instance");
			MyProxy myProxyInstance = new MyProxy(host, 7512);	
			System.out.println("created myproxy instance");
			cred = myProxyInstance.get(null, username, password, 3000);
			//System.out.println(cred);
			//System.out.println(cred.getRemainingLifetime());
			status="success";
		}	
		catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			status = "error";
		}
	}
	
	/**
	@return the GSSCredential obtained from the server.
	*/
	public GSSCredential getCred() {
		return cred;
	}
	
	/**
	@return "success" if successfully logged on to my proxy server, "error" if there was a problem
	*/
	public String getStatus() {
		return status;
	}
}